using MyStatus;
using Services;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class StatusIcon : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    protected LinearTransformation linear;
    protected Image m_image;
    protected RuntimeStatus m_status;
    protected Services.EventSystem eventSystem;
    public static float s_offset = 10;

    static StatusIcon()
    {
        s_offset = s_offset / 800 * Screen.width;
    }

    private void Awake()
    {
        m_image = GetComponent<Image>();
        eventSystem = ServiceLocator.Get<Services.EventSystem>();
        linear = new LinearTransformation();
        linear.Tick += SetAlpha;
        m_image.color = m_image.color.SetAlpha(0);
    }

    public void SetStatus(RuntimeStatus status)
    {
        m_status = status;
        if (status == null)
        {
            linear.Initialize(m_image.color.a, 0, 0.1f);
        }
        else
        {
            m_image.sprite = status.statusAttached.icon;
            linear.Initialize(m_image.color.a, 1, 0.1f);
        }
    }

    private void SetAlpha(float value)
    {
        m_image.color = m_image.color.SetAlpha(value);
    }

    public virtual void OnPointerEnter(PointerEventData eventData)
    {
        if (m_status == null)
            return;
        string information = m_status.GetInformation();
        eventSystem.Invoke<string, Vector2, float>(EEvent.View, information, Camera.main.WorldToScreenPoint(transform.position), s_offset);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if (m_status == null)
            return;
        eventSystem.Invoke(EEvent.EndView);
    }
}
